package com.cy.pet.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.pet.common.mapper.ItemMapper;
import com.cy.pet.common.pojo.Item;
import com.cy.pet.common.service.ItemService;
import com.cy.pet.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/10/8 10:16
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 根据商品所属类型查询商品列表
     * @param id
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getData(Integer id,PageResult pageResult){
        Page<Item> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("item_cat_id",id);
        //利用MP的分页API 实现查询
        page = itemMapper.selectPage(page,queryWrapper);
        //获取total和list
        long total = page.getTotal();
        List<Item> list = page.getRecords();
        //封装成功之后原来3个参数   封装之后变为5个 之后返回
        return pageResult.setTotal(total).setRows(list);
    }

    /**
     * 根据关键字模糊查询商品列表
     * @param keyword
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getDataByKeyword(String keyword, PageResult pageResult) {
        Page<Item> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title",keyword);
        //利用MP的分页API 实现查询
        page = itemMapper.selectPage(page,queryWrapper);
        //获取total和list
        long total = page.getTotal();
        List<Item> list = page.getRecords();
        //封装成功之后原来3个参数   封装之后变为5个 之后返回
        return pageResult.setTotal(total).setRows(list);
    }

    @Override
    public Item getItemById(Integer itemId) {
        Item item = itemMapper.selectById(itemId);
        return item;
    }

    @Override
    public void storeDesc(Item item) {
        itemMapper.updateById(item);
    }
}
